package com.wing.company.controller.app;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.JsonResult;
import com.wing.config.BaseDataInit;
import com.wing.web.db.config.util.DynamicDataSource;
import com.wing.platform.feign.PlatformClient;
import com.wing.user.feign.UserClient;
import com.wing.user.model.response.CompanyInfoVO;
import com.wing.web.utils.JwtUtils;
import com.wing.web.utils.LocaleMessage;
import com.wing.common.utils.PageResult;
import com.wing.company.model.entity.Company;
import com.wing.company.model.response.CompanyVO;
import com.wing.company.service.CompanyService;
import io.swagger.annotations.Api;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.wing.company.model.request.CompanyForm;
import org.springframework.validation.annotation.Validated;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author
 */
@Validated
@Api(value = "App-管理", tags = "管理")
@RestController("appCompanyController")
@RequestMapping("company/app/company")
public class CompanyController {

    @Autowired
    private CompanyService companyService;
    @Autowired
    private LocaleMessage localeMessage;
    @Autowired
    private PlatformClient platformClient;
    @Autowired
    private UserClient userClient;

    @GetMapping()
    public PageResult<CompanyVO> list(
            @RequestParam(name = "page", value = "第几页", defaultValue = "1", required = false) Integer page
            ,@RequestParam(name = "limit", value = "每页多少条", defaultValue = "10", required = false) Integer limit
            ,@RequestParam(name = "sort", value="排序字段名", defaultValue = "create_time", required = false) String sort
            ,@RequestParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "desc", required = false) String order
    ) {

        Page<CompanyVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }

        Page<CompanyVO> list = companyService.pageList4app(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @GetMapping("/{id}")
    public JsonResult detail(@PathVariable("id") Long companyId) {

        CompanyVO companyVO = companyService.get4appById(companyId);
        if (companyVO == null) {
            return JsonResult.fail(localeMessage.getMessage("common.fail"));
        }
        return JsonResult.success(localeMessage.getMessage("common.success"), companyVO);
    }

    @PostMapping()
    public JsonResult add(@Validated CompanyForm companyForm) {

        if (companyService.add(companyForm) > 0) {
            return JsonResult.success(localeMessage.getMessage("common.success"));
        }
        return JsonResult.fail(localeMessage.getMessage("common.fail"));
    }

    /**
     * 根据id查询企业信息
     * @param companyId 企业id
     * @return
     */
    @GetMapping("/getCompanyInfo")
    public JsonResult getCompanyInfo(@RequestParam String companyId){

        Company company = companyService.getById(companyId);
        return JsonResult.success(company);
    }

    @GetMapping("/getCompany")
    public JsonResult getCompany(){
        JsonResult companyInfoByCode = platformClient.getCompanyInfoByCodeApp(BaseDataInit.ENTERPRISE_DATA.getCode());
        return companyInfoByCode;
    }
}
